#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <signal.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>

char buf[100];
int sockfd, connfd, len;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;

void sig_io(int signo) {
	printf("sigio\n");
	recvfrom(sockfd, buf, 100, 0, (struct sockaddr*)&client_addr, &len);
	printf("svr_read: %s\n", buf);
}

int main() {
	len = sizeof(server_addr);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	server_addr.sin_port = htons(4567);

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	bind(sockfd, (struct sockaddr*)&server_addr, len);

	int flag = 0;
	fcntl(sockfd, F_GETFL, 0);
	flag |= O_NONBLOCK;
	flag |= O_ASYNC;
	fcntl(sockfd, F_SETFL, flag);
	fcntl(sockfd, F_SETOWN, getpid());

	sigset_t  zeroset;
	sigfillset(&zeroset);
	sigdelset(&zeroset, SIGIO);

	while(1) {
		sigsuspend(&zeroset);
		sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&client_addr, len);
	}

	close(sockfd);
	return 0;
}